import java.util.concurrent.TimeUnit;

/**
 * @author lzm
 * @date 2025/2/20 20:02
 * @description 多线程版的累加器
 */
public class AdderV2 {

    int i = 0;

    /**
     * 多线程版的累加器
     */
    public void add() throws InterruptedException {
        Runnable task = () -> {
            // 将变量i加1，重复这个操作10000次
            for (int j = 0; j < 10000; j++) {
                i++;
            }
        };
        for (int j = 0; j < 5; j++) {
            new Thread(task, "线程-" + j).start();
        }
        TimeUnit.SECONDS.sleep(1);
    }


    public static void main(String[] args) throws InterruptedException {
        AdderV2 adder = new AdderV2();
        adder.add();
        System.out.println("计算结果是：" + adder.i);
    }

}
